package com.huike.clues.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.clues.mapper.ActivityMapper;
import com.huike.clues.mapper.CluesRecordMapper;
import com.huike.clues.mapper.MpAssignRecordMapper;
import com.huike.clues.mapper.MpClueMapper;
import com.huike.clues.service.CluesRecordService;
import com.huike.common.entity.MPTbActivity;
import com.huike.common.entity.MPTbAssignRecord;
import com.huike.common.entity.MPTbClue;
import com.huike.common.entity.MPTbClueTrackRecord;
import com.huike.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Description TODO
 * @Author hxd
 * @Date 2023-08-30 14:27
 */
@Service
public class CluesRecordServiceImpl extends ServiceImpl<CluesRecordMapper, MPTbClueTrackRecord> implements CluesRecordService {
    @Resource
    private CluesRecordMapper cluesRecordMapper;

    @Autowired
    private MpClueMapper mpClueMapper;

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private MpAssignRecordMapper mpAssignRecordMapper;

    /**
     * 获取线索跟进记录详细信息
     * @param id
     * @return
     */
    @Override
    public MPTbClueTrackRecord findId(Long id) {
        QueryWrapper<MPTbClueTrackRecord> wrapper = new QueryWrapper();
        wrapper.eq("clue_id",id);
        MPTbClueTrackRecord mpTbClueTrackRecord = cluesRecordMapper.selectOneaa(id);
        if (mpTbClueTrackRecord != null) {




        MPTbClue mpTbClue = mpClueMapper.findId(id);

        if (!Objects.isNull(mpTbClue)) {
            //手机号
            mpTbClueTrackRecord.setPhone(mpTbClue.getPhone());
            //渠道来源
            mpTbClueTrackRecord.setChannel(mpTbClue.getChannel());
            //活动信息
            QueryWrapper<MPTbActivity> wrapper1 = new QueryWrapper();
            wrapper1.eq("id", mpTbClue.getActivityId());


            MPTbActivity mpTbActivity = activityMapper.selectOnea(mpTbClue.getActivityId());

            mpTbClueTrackRecord.setActivityInfo(mpTbActivity.getName() + mpTbActivity.getInfo());
            //线索归属
            MPTbAssignRecord mpTbAssignRecord = mpAssignRecordMapper.selectOnea(id);


        if(StringUtils.isNotEmpty(mpTbAssignRecord.getUserName())){
            mpTbClueTrackRecord.setOwner(mpTbAssignRecord.getUserName());
            mpTbClueTrackRecord.setOwnerTime(mpTbAssignRecord.getCreateTime());  //归属时间
        }
        }
        }
        return mpTbClueTrackRecord;
    }

    /**
     * 查询线索跟进记录列表
     * @param clueld
     * @return
     */
    @Override
    public MPTbClueTrackRecord recordList(Long clueld) {
//        QueryWrapper<MPTbClueTrackRecord> wrapper = new QueryWrapper();
//        wrapper.eq("clue_id",clueld);
        MPTbClueTrackRecord mpTbClueTrackRecord = cluesRecordMapper.selectOneaa(clueld);
        return mpTbClueTrackRecord;
    }

    /**
     * 新增线索跟进记录
     * @param mpTbClueTrackRecord
     */
    @Override
    public void add(MPTbClueTrackRecord mpTbClueTrackRecord) {
        cluesRecordMapper.insert(mpTbClueTrackRecord);
    }
}
